package com.wlwx.simulator.codec;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;

import java.nio.ByteBuffer;

public class Encoder extends MessageToByteEncoder<CMPPCommand> {

    protected void encode(ChannelHandlerContext ctx, CMPPCommand command, ByteBuf out) throws Exception {
        try {
            byte[] body = command.encodeBody();

            ByteBuffer byteBuffer = command.encodeHeader(body.length);
            out.writeBytes(byteBuffer);

            if(body != null && body.length > 0) {
                out.writeBytes(body);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
